package com.xlh.security;

import com.google.common.collect.Lists;
import com.xlh.enums.user.EventCodeEnum;
import com.xlh.pojo.system.Operation;
import com.xlh.service.system.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lx 2020/06/12
 */
@Component
public class LogoutHandler implements org.springframework.security.web.authentication.logout.LogoutHandler {

    @Autowired
    private OperationService operationService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Override
    public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
        List<Operation> operations = Lists.newArrayList(convertOperation());
        operationService.insertBatch(operations);
    }

    private Operation convertOperation() {
        Operation operation = new com.xlh.pojo.system.Operation();
        operation.setEventCode(EventCodeEnum.LOGOUT.getCode());
        operation.setEventDesc(EventCodeEnum.LOGOUT.getMessage());
        operation.setRole(userInfoHolder.getUser().getRole());
        operation.setUserId(userInfoHolder.getUserId());
        return operation;
    }

}
